package cn.lili.modules.order.order.entity.dto;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.order.order.entity.dos.FlowReviewLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

/**
 * 异常流水操作日志
 *
 * @author Chopper
 * @version v1.0
 * 2021-12-08 10:53
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class FlowReviewLogQueryDTO extends PageVO implements Serializable {

    @ApiModelProperty(value = "订单编号")
    private String orderSn;

    @ApiModelProperty(value = "流水编号")
    private String orderFlowSn;

    @ApiModelProperty(value = "店铺ID")
    private String storeId;

    @ApiModelProperty(value = "供应商ID")
    private String supplierId;

    @ApiModelProperty(value = "开始时间")
    private String startDate;

    @ApiModelProperty(value = "结束时间")
    private String endDate;


    /**
     * 生成查询wrapper
     * <p>
     *
     * @return 查询wrapper
     */
    public LambdaQueryWrapper<FlowReviewLog> generatorQueryWrapper() {


        LambdaQueryWrapper<FlowReviewLog> lambdaQueryWrapper = Wrappers.lambdaQuery();
        //订单编号
        lambdaQueryWrapper.eq(CharSequenceUtil.isNotEmpty(this.getOrderSn()), FlowReviewLog::getOrderSn, this.getOrderSn());
        //订单编号
        lambdaQueryWrapper.eq(CharSequenceUtil.isNotEmpty(this.getOrderFlowSn()), FlowReviewLog::getOrderFlowSn, this.getOrderFlowSn());

        //店铺ID
        lambdaQueryWrapper.eq(CharSequenceUtil.isNotEmpty(this.getStoreId()), FlowReviewLog::getOperatorId, this.getStoreId());

        // 判断城市站点
        if (SceneEnums.URBAN_SUBSTATION.equals(Objects.requireNonNull(UserContext.getCurrentUser()).getScene())) {
            lambdaQueryWrapper.eq(FlowReviewLog::getOperatorId, UserContext.getCurrentUser().getExtendId());
        }
        //支付时间 开始时间和结束时间
        if (!StringUtils.isEmpty(this.getStartDate()) && !StringUtils.isEmpty(this.getEndDate())) {
            Date start = DateUtil.parse(this.getStartDate());
            Date end = DateUtil.parse(this.getEndDate());
            lambdaQueryWrapper.between(FlowReviewLog::getCreateTime, start, end);
        }

        lambdaQueryWrapper.orderByDesc(FlowReviewLog::getCreateTime);
        return lambdaQueryWrapper;
    }

}
